﻿import mx.utils.Delegate;

import com.jxl.shuriken.core.UIComponent;
import com.jxl.shuriken.utils.DrawUtils;
import com.jxl.shuriken.events.ShurikenEvent;
import com.jxl.shuriken.events.Callback;

class com.jxl.shuriken.controls.SimpleButton extends UIComponent
{
	public static var SYMBOL_NAME:String = "com.jxl.shuriken.controls.SimpleButton";
	
	private var __mouseListener:Object;
	private var __pressCallback:Callback;
	private var __releaseCallback:Callback;
	private var __releaseOutsideCallback;
	private var __rollOverCallback:Callback;
	private var __rollOutCallback:Callback;
	private var __mouseDownOutsideCallback:Callback;
	
	
	public function SimpleButton()
	{
		super();
		
		if(__mouseListener == null)
		{
			__mouseListener = {};
			__mouseListener.onMouseDown = Delegate.create(this, onMouseDownSomewhere);
			Mouse.addListener(__mouseListener);
		}
	}
	
	private function redraw():Void
	{
		super.redraw();
		
		drawButton();
	}
	
	public function drawButton():Void
	{
		/*
		clear();
		lineStyle(0, 0x999999);
		beginFill(0xCCCCCC);
		DrawUtils.drawRoundRect(this, 0, 0, width, height, 6);
		endFill();
		*/
		
		clear();
		lineStyle(2, 0x003C74);
		var fil:String = "linear";
		var clrs:Array = [0xFFFFFF, 0xC6C5D7];
		var alph:Array = [100, 100];
		var rati:Array = [90, 255];
		var mat:Object = { matrixType:"box", x:0, y:0, w:__width, h:__height, r:(90/180)*Math.PI };
		beginGradientFill(fil, clrs, alph, rati, mat);
		DrawUtils.drawRoundRect(this, 0, 0, __width, __height, 3);
		endFill();
	}
	
	public function onPress():Void
	{
		__pressCallback.dispatch(new ShurikenEvent(ShurikenEvent.PRESS, this));
		
	}
	
	public function onRelease():Void
	{
		//DebugWindow.debugHeader();
		//DebugWindow.debug("SimpleButton::onRelease");
		//DebugWindow.debug("__releaseCallback: " + __releaseCallback);
		__releaseCallback.dispatch(new ShurikenEvent(ShurikenEvent.RELEASE, this));
	}
	
	public function onReleaseOutside():Void
	{
		__releaseOutsideCallback.dispatch(new ShurikenEvent(ShurikenEvent.RELEASE_OUTSIDE, this));
	}
	
	public function onRollOver():Void
	{
		__rollOverCallback.dispatch(new ShurikenEvent(ShurikenEvent.ROLL_OVER, this));
	}
	
	public function onRollOut():Void
	{
		__rollOutCallback.dispatch(new ShurikenEvent(ShurikenEvent.ROLL_OUT, this));
	}
	
	
	private function onMouseDownSomewhere():Void
	{
		var p:Object = {};
		p.x = _xmouse;
		p.y = _ymouse;
		
		localToGlobal(p);
		
		if (hitTest(p.x, p.y) == false)
		{
			__mouseDownOutsideCallback.dispatch(new ShurikenEvent(ShurikenEvent.MOUSE_DOWN_OUTSIDE, this));
		}
	}
	
	public function setPressCallback(scope:Object, func:Function):Void
	{
		__pressCallback = new Callback(scope, func);
	}
	
	public function setReleaseCallback(scope:Object, func:Function):Void
	{
		if(scope == null)
		{
			delete __releaseCallback;
			return;
		}
		__releaseCallback = new Callback(scope, func);
	}
	
	public function setReleaseOutsideOutside(scope:Object, func:Function):Void
	{
		__releaseOutsideCallback = new Callback(scope, func);
	}
	
	public function setRollOverCallback(scope:Object, func:Function):Void
	{
		__rollOverCallback = new Callback(scope, func);
	}
	
	public function setRollOutCallback(scope:Object, func:Function):Void
	{
		__rollOutCallback = new Callback(scope, func);
	}
	
	public function setMouseDownOutsideCallback(scope:Object, func:Function):Void
	{
		__mouseDownOutsideCallback = new Callback(scope, func);
	}
	
	
}